package be.vdab.servlets;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.dao.PizzaDAO;
import be.vdab.entities.Pizza;
import be.vdab.util.RequestStatistiek;

/**
 * Servlet implementation class PizzaServlet
 */
@WebServlet("/menu/pizzas.htm")
public class PizzaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String VIEW = "/WEB-INF/JSP/pizzas.jsp";
	private final RequestStatistiek requestStatistiek = new RequestStatistiek();
	private final PizzaDAO pizzaDAO = new PizzaDAO();
	
	public PizzaServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setAttribute("sterren", 5);

		/* Map<Long, Pizza> pizzas = new LinkedHashMap<Long, Pizza>();
		pizzas.put(12L, new Pizza(12, "Prosciutto", new BigDecimal(4), true));
		pizzas.put(14L, new Pizza(14, "Margherita", new BigDecimal(5), false));
		pizzas.put(17L, new Pizza(17L, "Calzone", new BigDecimal(4), false));
		pizzas.put(23L, new Pizza(23L, "Fungi & Olive", new BigDecimal(5),
				false)); */
		
		List<Pizza> pizzas = pizzaDAO.findAll();
		String pizzaFotosPad = this.getServletContext().getRealPath("/pizzafotos");
		Set <Long> pizzaNummersMetFoto = new HashSet<Long>();
		for (Pizza pizza: pizzas){
			File file = new File(pizzaFotosPad + "/" + pizza.getNummer()+ ".jpg");
			if (file.exists()){
				pizzaNummersMetFoto.add(pizza.getNummer());
			}
		}
		
		requestStatistiek.voegRequestToe(request);
		request.setAttribute("pizzas", pizzas);
		RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
		dispatcher.forward(request, response);
	}

}
